/**
 * @param string value
 * @param HtmlElement element
 * @param mixed param
 */
Rule.date = function( value ) 
{
	if( value == "" ){ 
  		return true;
  	}
	
  	var regex = value.match( /^((?:0[1-9]|[1-2][0-9])|[3][0-1])\/(0[1-9]|[1][0-2])\/((?:19|[2-9][0-9])\d{2})$/ );
  	
	if ( regex == null ){
		return false;
	}
	
	if( regex[2] == 2 && regex[1] > 29 ){
		return false;
	}
	
	if( regex[2] == 2 && regex[1] == 29 && ( regex[3] % 4 ) ){
		return false;
	}
	
	if( 
		 ( 
		   regex[2] == 4 || 
		   regex[2] == 6 || 
		   regex[2] == 9 || 
		   regex[2] == 11 
		 ) && 
		 ( regex[1] == 31 ) 
	 )
	 {
    	return false;
	 }
	
	return true;
}
/**
 * mask corresponding date
 */
Rule.date.mask = function( id )
{
    $('#'+id).unmask().mask("99/99/9999");
}

Rule.dateGreaterThan = function( value, date ) 
{	
	return Rule._dateGreaterLessThan( Rule.greaterThan,  value, date );
}

Rule.dateLessThan = function( value, date ) 
{
	return Rule._dateGreaterLessThan( Rule.lessThan, value, date );
}

Rule._dateGreaterLessThan = function( fn, value, date )
{
	try{
		if( date == "" || value == "" ){
			return true;
		}
		
		value = Rule._dateTransform( value );
		date  = Rule._dateTransform( date );
		
		if( !( value && date ) ){
			return false;
		}
		
		return fn( value, date );
		
	}catch(e){
		return true;
	}
}

Rule._dateTransform = function( value )
{
	if( !Rule.date( value ) ){
		return false;
	}
	
	value = value.split( "/" );
	return parseInt( value[2] + value[1] + value[0] );
}

Rule.dateBetween = function( value, dateStart, dateEnd ) 
{
	try{
		if( value == "" ){
			return true;
		}
		
		value 	  = Rule._dateTransform( value );
		dateStart = Rule._dateTransform( dateStart ); 
		dateEnd   = Rule._dateTransform( dateEnd );
		
		if( !( value && dateStart && dateEnd ) ){
			return false;
		}
		
		return Rule.between( value, dateStart, dateEnd );
	}catch( e ){
		return true;
	}
}